//package com.charley.icloud.admin.controller;
//
//import cn.hutool.core.bean.BeanUtil;
//import cn.hutool.core.bean.copier.CopyOptions;
//import com.charley.icloud.admin.pojo.request.LoginQuery;
//import com.charley.icloud.admin.pojo.request.SysUserQuery;
//import com.charley.icloud.admin.security.JwtAuthenticationToken;
//import com.charley.icloud.admin.security.SecurityUtils;
//import com.charley.icloud.admin.utils.RandomAccountGenerator;
//import com.charley.icloud.admin.webapps.sysuser.pojo.entity.SysUserDO;
//import com.charley.icloud.admin.webapps.sysuser.service.SysUserService;
//import com.charley.icloud.common.response.code.SystemCode;
//import com.charley.icloud.office.utils.IOUtils;
//import com.charley.icloud.security.utils.PasswordUtils;
//import com.google.code.kaptcha.Constants;
//import com.google.code.kaptcha.Producer;
//import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.Parameter;
//import io.swagger.v3.oas.annotations.enums.ParameterIn;
//import io.swagger.v3.oas.annotations.tags.Tag;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.util.ObjectUtils;
//import org.springframework.web.bind.annotation.*;
//
//import javax.annotation.Resource;
//import javax.imageio.ImageIO;
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.validation.Valid;
//import java.awt.image.BufferedImage;
//import java.io.IOException;
//import java.util.Date;
//
//@Slf4j
//@RestController
//@RequestMapping("/auth")
//@Tag(name = "用户登录控制器")
//public class SysLoginController {
//
//    // 标识在哪个类里面打印的日志
//    //final Logger logger = LoggerFactory.getLogger(SysLoginController.class.getClass());
//
//    @Resource
//    private Producer producer;
//
//    @Resource
//    private SysUserService sysUserService;
//
//    @Resource
//    private AuthenticationManager authenticationManager;
//
//    @GetMapping("/captcha.jpg")
//    @Operation(summary = "获取验证码", description = "后台管理系统")
//    public void captcha(HttpServletResponse response, HttpServletRequest request) throws IOException {
//        // 设置响应头
//        response.setHeader("Cache-Control", "no-store, no-cache");
//        response.setContentType("image/jpeg");
//
//        // 生成文字验证码
//        String text = producer.createText();
//        // 生成图片验证码
//        BufferedImage image = producer.createImage(text);
//        // 保存到验证码到 session
//        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, text);
//        log.info("验证码 ---> {}", text);
//
//        ServletOutputStream out = response.getOutputStream();
//        ImageIO.write(image, "jpg", out);
//        IOUtils.closeQuietly(out);
//    }
//
//    @PostMapping("/register")
//    @Operation(summary = "新用户注册", description = "后台管理系统")
//    public HttpResult registerNewUser(HttpServletRequest request, HttpServletResponse response,
//                                      @Valid @RequestBody SysUserQuery newUserInfo) {
//        SysUserDO sysUserDO = new SysUserDO();
//        if (ObjectUtils.isEmpty(newUserInfo)){
//            log.error("system user registerNewUser SysUserQuery is null");
//            HttpResult httpResult = HttpResult.error(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_PARAM_NULL,SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_PARAM_NULL_MSG);
//            log.info("system user addUser return msg: " + httpResult);
//            return httpResult;
//        }
//        if (newUserInfo != null) {
//            if (StringUtils.isBlank(newUserInfo.getUsername())) {
//                log.error("system user registerNewUser SysUserQuery is null");
//                HttpResult httpResult = HttpResult.error(SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_NAME_NULL,SystemCode.SYSTEM_USER_ERROR_ADD_FAIL_NAME_NULL_MSG);
//                log.info("system user addUser return msg: " + httpResult);
//                return httpResult;
//            }
//            if (StringUtils.isBlank(newUserInfo.getPassword())) {
//                log.error("system user registerNewUser uname is null");
//                log.info("param：" + newUserInfo);
//                return HttpResult.error("密码不能为空");
//            }
//            BeanUtil.copyProperties(newUserInfo, sysUserDO, CopyOptions.create().ignoreCase().ignoreNullValue());
//            // 完善数据
//            String randomAccount = RandomAccountGenerator.generateRandomAccount();
//            SysUserDO account = sysUserService.findByAccount(randomAccount);
//            if(account == null || account.getId() == null ){
//                sysUserDO.setAccount(RandomAccountGenerator.generateRandomAccount()); // 随机账号生成
//            }
//            sysUserDO.setSalt(PasswordUtils.getSalt()); // 16位随机盐生成
//            sysUserDO.setPassword(PasswordUtils.encode(sysUserDO.getPassword(),sysUserDO.getSalt()));
//            sysUserDO.setCreateTime(new Date());
//            sysUserDO.setLastUpdateTime(new Date());
//            sysUserDO.setCreateBy("System");
//            sysUserDO.setLastUpdateBy("System");
//            if(sysUserService.save(sysUserDO) == 1){
//                return HttpResult.ok("注册成功，已为您生成账号："+randomAccount+", 请您牢记密码并尽快修改账号，祝您生活愉快...");
//            }
//        }
//        return HttpResult.error("注册信息不正确，请检查后重试...");
//
//    }
//
//    @PostMapping(value = "/login")
//    @Operation(summary = "登录接口", description = "后台管理系统")
//    @Parameter(name = "loginQuery", description = "账号 密码 验证码", required = true, in = ParameterIn.QUERY)
//    public HttpResult login(@RequestBody LoginQuery loginQuery, HttpServletRequest request) {
//        String account = loginQuery.getAccount();
//        String password = loginQuery.getPassword();
//        String captcha = loginQuery.getCaptcha();
//        // 从session中获取之前保存的验证码跟前台传来的验证码进行匹配
//        Object kaptcha = request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
//        if (kaptcha == null) {
//            return HttpResult.error("验证码已失效");
//        }
//        if (!captcha.equals(kaptcha)) {
//            return HttpResult.error("验证码不正确");
//        }
//        // 通过账号登录
//        SysUserDO user = sysUserService.findByAccount(account);
//        // 账号不存在、密码错误
//        if (user == null) {
//            return HttpResult.error("账号不存在");
//        }
//        if (!PasswordUtils.matches(user.getSalt(), password, user.getPassword())) {
//            return HttpResult.error("密码不正确");
//        }
//        // 账号锁定
//        if (user.getStatus() == 0) {
//            return HttpResult.error("账号已被锁定,请联系管理员");
//        }
//        // 系统登录认证
//        JwtAuthenticationToken token = SecurityUtils.login(request, account, password, authenticationManager);
//        return HttpResult.ok(token);
//    }
//
//}
